package com.jcp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jcp.dao.NoticesDao;
import com.jcp.dao.userAndFileDao;
import com.jcp.daoimp.NoticesDaoImp;
import com.jcp.daoimp.userAddFileDao;
import com.jcp.entity.Attences;
import com.jcp.entity.Departments;
import com.jcp.entity.DepartmentsAndUserName;
import com.jcp.entity.Files;
import com.jcp.entity.FilesAndUserName;
import com.jcp.entity.FilesAndUsers;
import com.jcp.entity.FilesMin;
import com.jcp.entity.Notices;
import com.jcp.entity.Posts;
import com.jcp.service.AttencesService;
import com.jcp.service.DepartmentsService;
import com.jcp.service.FilesService;
import com.jcp.service.NoticesService;
import com.jcp.service.postServerDao;
import com.jcp.service.userAddFileServer;
import com.jcp.serviceimp.AttencesServiceImp;
import com.jcp.serviceimp.DepartmentsServiceImp;
import com.jcp.serviceimp.FilesServiceImp;
import com.jcp.serviceimp.NoticesServiceImp;
import com.jcp.serviceimp.posServerImple;
import com.jcp.serviceimp.userAndFileImple;
import com.jcp.util.DBUtil;
import com.jcp.util.MD5Util;
import com.jcp.util.PageData;

/**
 * Servlet implementation class NoticesServlet
 */
@WebServlet("/ns.do")
public class NoticesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	NoticesService ns = new NoticesServiceImp();//创建公告服务层的对象
    FilesService fs = new FilesServiceImp(); 
    userAddFileServer uafs = new userAndFileImple();
    postServerDao psd = new posServerImple();
AttencesService as = new AttencesServiceImp();
    
    DepartmentsService ds = new DepartmentsServiceImp();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NoticesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码
		request.setCharacterEncoding("utf-8");
		//设置响应编码
		response.setCharacterEncoding("utf-8");
			//创建一个op的字符串进行判断功能选择
			String op = "";
			//判断request.getParameter("op")是否有值：有值则对op进行赋值
			if(request.getParameter("op1")!=null) {
				//op赋值
				op = request.getParameter("op1");
			}
			//登入操作
			if(op.equals("login")) {
				//对Notices数据库进行全查询
				selQuery(request, response);
			}
			//公告管理的添加功能
			if(op.equals("add")) {
				//获取add表单中的公告内容
				String notContext = request.getParameter("username1");
				//获取add表单中的公告时间
				String notRepass = request.getParameter("repass1");
				//将数据封装到Notices中
				Notices notices = new Notices(notContext, 0, notRepass);
				
				boolean flag = ns.addNotices(notices);
				
			}
				//公告管理的添加功能由部长发布
			else  if(op.equals("add2")) {
					//获取add表单中的公告标题
					String title = request.getParameter("title");
					//获取add表单中的公告内容&userId=${sessionScope.userMessage.userId}
					System.out.println("标题"+title);
					String desc = request.getParameter("desc");
					String  notContext=title+desc ;
					//还需将当前sessionon储存的对象的posId
					//将数据封装到Notices中
					Notices notices = new Notices(notContext, 2, null);
					
					boolean flag = ns.addNotices(notices);
					//PrintWriter out =response.getWriter();
					//out.print(out);
					//out.close();
					request.getRequestDispatcher("min.do").forward(request, response);
					
			}else if(op.equals("select")) {//公告管理的查询功能
				/*//创建一个获取表单参数数据变量
				String notContent = "";
				//判断表单数据是否为空值
				if(request.getParameter("username2")!=null) {
					
					notContent = request.getParameter("username2");
				}
				
				List<Notices> list = ns.selNotices(notContent,1);
				//存储list<Notices>的属性	
				request.setAttribute("notices", list);
				//Notices_1.js跳转
				request.getRequestDispatcher("Notices_1.jsp").forward(request, response);*/
				int curr = 1;
				int limit = 7;
				String strLike = "";
				if(request.getParameter("curr")!=null) {
					curr = Integer.parseInt(request.getParameter("curr"));
				}
				if(request.getParameter("limit")!=null) {
					limit = Integer.parseInt(request.getParameter("limit"));
				}
				if(request.getParameter("username2")!=null) {
					strLike = request.getParameter("username2");
				}
				NoticesDao nd = new NoticesDaoImp();
				PageData<Notices> pd  = nd.selNoticesPage(curr,limit,strLike);
				System.out.println("List : "+pd.getData());
				request.setAttribute("notices", pd);
				request.getSession().setAttribute("strLike", strLike);
				request.getRequestDispatcher("Notices_1.jsp").forward(request, response);
				
			}else if(op.equals("filesadd")) {
				//这是档案增加的方法
				//获得 files_1添加档案页面的text里面的数据
				String nation= request.getParameter("nation");
				String address=request.getParameter("address");
				int cardId=Integer.parseInt(request.getParameter("cardId"));
				String sex=request.getParameter("sex");
				String birthday=request.getParameter("birthday");
				String political=request.getParameter("political");
				String isMarried=request.getParameter("isMarried");
				String school=request.getParameter("school");
				String graduateTime=request.getParameter("graduateTime");
				String major=request.getParameter("major");
				String phone=request.getParameter("phone");
				String blood=request.getParameter("blood");
				double weight=Double.parseDouble(request.getParameter("weight"));
				double height=Double.parseDouble(request.getParameter("height"));
				String education=request.getParameter("education");
				String cv1=request.getParameter("cv1");
				String cv2=request.getParameter("cv2");
				String cv3=request.getParameter("cv3");
				String photosurl = request.getParameter("photosurl");
				//实例化对象
				Files files = new Files(sex,nation,address,birthday,cardId,political,isMarried,school,graduateTime,major,cv1,
						cv2,cv3,phone,blood,weight,height,education,photosurl);
				
				boolean flag= fs.addFiles(files);
				
				if(flag) {
					System.out.println("操作成功");
				}else {
					System.out.println("操作失败");
				}
				
		}else if(op.equals("fileswheresel")) {
			//这是档案的全查询的
			int fileId = Integer.valueOf(request.getParameter("fileId"));
			List<FilesAndUserName> list= fs.selFiles(fileId);
//			//3 传递数据
			request.setAttribute("list",list);
//			//4 跳转到jsp
			request.getRequestDispatcher("files_2edit.jsp").forward(request, response);
			
		}else if(op.equals("filesdel")){
			//这是档案删除的
			int fileId = Integer.valueOf(request.getParameter("fileId"));
			boolean flag = fs.delFiles(fileId);
			if(flag) {
				System.out.println("操作成功");
			}else {
				System.out.println("操作失败");
			}
			//删除档案，但是功能用不上，待定
			int page=1;
			int pageSize=10;
			String strLike = "";
			if(request.getParameter("page")!=null) {
				page=Integer.valueOf(request.getParameter("page"));
			}
			if(request.getParameter("pageSize")!=null) {
				pageSize=Integer.valueOf(request.getParameter("pageSize"));
			}
			if (request.getParameter("strLike") != null) {
				strLike = request.getParameter("strLike");
			}
			PageData<FilesMin> pageData = fs.getselFilesminMinPage(page, pageSize);
//			System.out.println("pageData"+pageData+"pageSize "+pageSize);
			request.setAttribute("pageData",pageData);
			request.setAttribute("strLike",strLike);
			request.getRequestDispatcher("files_2.jsp").forward(request, response);
		}else if(op.equals("filesup")) {
			//获得file_2edit页面上的值	
			int fileId=Integer.valueOf(request.getParameter("fileId"));
			String nation= request.getParameter("nation");
			String address=request.getParameter("address");
			int cardId=Integer.parseInt(request.getParameter("cardId"));
			String sex=request.getParameter("sex");
			String birthday=request.getParameter("birthday");
			String political=request.getParameter("political");
			String isMarried=request.getParameter("isMarried");
			String school=request.getParameter("school");
			String graduateTime=request.getParameter("graduateTime");
			String major=request.getParameter("major");
			String phone=request.getParameter("phone");
			String blood=request.getParameter("blood");
			double weight=Double.parseDouble(request.getParameter("weight"));
			double height=Double.parseDouble(request.getParameter("height"));
			String education=request.getParameter("education");
			String cv1=request.getParameter("cv1");
			String cv2=request.getParameter("cv2");
			String cv3=request.getParameter("cv3");
//			String photosurl = request.getParameter("photosurl");
			String photosurl = request.getContextPath()+"/zhaopian/"+9+".jsp";
			//实例化对象，把值放进去
			Files files = new Files(sex,nation,address,birthday,cardId,political,isMarried,school,graduateTime,major,cv1,
			cv2,cv3,phone,blood,weight,height,education,photosurl,fileId);
			
			boolean flag= fs.updFiles(files);
			
			int curr = 1;
			int limit = 5;
			String strLike = "";
			if(request.getParameter("curr")!=null) {
				curr = Integer.parseInt(request.getParameter("curr"));
			}
			if(request.getParameter("limit")!=null) {
				limit = Integer.parseInt(request.getParameter("limit"));
			}
			if(request.getParameter("username2")!=null) {
				strLike = request.getParameter("username2");
			}
			NoticesDao nd = new NoticesDaoImp();
			PageData<FilesMin> pd  = fs.getselFilesminMinPage1(curr, limit, strLike);
			System.out.println("List : "+pd.getData());
			request.setAttribute("notices", pd);
			request.getSession().setAttribute("strLike", strLike);
			request.getRequestDispatcher("files_2.jsp").forward(request, response);
		}else if(op.equals("deptsel")) {
			//对部门表进行全查询
			List<DepartmentsAndUserName> list = ds.Departmentssel();
			//存储list<Notices>的属性
			request.setAttribute("list",list);
			//Notices_1.js的跳转
			request.getRequestDispatcher("department_1.jsp").forward(request, response);
		}else if(op.equals("deptlocalsel")){
			int deptId = Integer.parseInt(request.getParameter("deptId"));
			//对部门表进行条件查询
			List<Departments> list = ds.Departmentslocalsel(deptId);
			System.out.println(list);
			//存储list<Notices>的属性
			request.setAttribute("list",list);
			//Notices_1.js的跳转
			request.getRequestDispatcher("departmentsedit_1.jsp").forward(request, response);
		}else if(op.equals("deptup")) {
			int deptId = Integer.parseInt(request.getParameter("deptId"));
			String deptName = request.getParameter("deptName");
			int deptManId = Integer.parseInt(request.getParameter("deptManId"));
			String deptInfo=request.getParameter("deptInfo");
			
			Departments dept = new Departments(deptName,deptManId,deptInfo,deptId);
			boolean f = ds.Departmentsup(dept);
			
			
			
		}else if(op.equals("deptdel")) {
			int deptId = Integer.parseInt(request.getParameter("deptId"));
			boolean f =  ds.Departmentsdel(deptId);
			
		}else if(op.equals("NoticesUpdate")) {//公告管理的修改
			//获取公告更改页面上的公告编号
			String notId = request.getParameter("NoticesId");
			//获取公告更改页面上的公告的时间
			String notTime = request.getParameter("NoticesTime");
			//获取公告更改页面上的公告的内容
			String notContent = request.getParameter("NoticesContent");

			SimpleDateFormat sdnf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
			
			try {
				//将公告的数据进行封装成Notices
				Notices notices = new Notices(Integer.parseInt(notId), notTime, notContent);

				boolean flag = ns.updsNotices(notices);
				System.out.println(flag);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}else if(op.equals("NoticesDel")) {//公告管理的删除
			int NoticesId = 0;//设置一个NoticesId的变量
			//判断获取来自Notices_1.jsp的编号参数是否为空值
			if(request.getParameter("NoticesId")!=null) {
				//设置编号变量
				NoticesId = Integer.parseInt(request.getParameter("NoticesId"));
			}
			//传入sql语句的返回boolean
			boolean flag = ns.delNotices(NoticesId);
		
		}else if(op.equals("Usersel")) {//--员工管理查询
			//创建一个获取表单参数数据变量
			String notContent = "";
			//判断表单数据是否为空值
			if(request.getParameter("username")!=null) {
				
				notContent = request.getParameter("username");
			}
			
			List<FilesAndUsers> FilesAndUsers = fs.Filesel(notContent);
			List<Departments> post = uafs.postSel();
			//部门的查询
			request.getSession().setAttribute("post1",post);
			//存储list<Notices>的属性	
			request.setAttribute("users", FilesAndUsers);
			//Notices_1.js跳转
			request.getRequestDispatcher("Users_1.jsp").forward(request, response);
		}else if(op.equals("useradd")) {//--员工管理添加
			//获取员工姓名
			String userName = request.getParameter("username");
			//获取员工性别
			String userSex = request.getParameter("sex");
			//获取员工手机
			String userPhone = request.getParameter("userphone");
			//获取员工邮箱 
			String userEmail = request.getParameter("userEmail");
			//获取员工地址
			String userAddress = request.getParameter("userAddress");
			//获取员工身份证
			String userCardId = request.getParameter("userCardId");
			//获取员工所属部门
			String userPost = request.getParameter("userPost");
			//获取员工密码
			String userPwd = request.getParameter("userPwd");
			String userPwd2 = MD5Util.getEncodeByMd5(userPwd);//对密码进行加密	
			FilesAndUsers fau = new FilesAndUsers(userName,userSex,userPhone,userEmail,userAddress,userCardId,userPost);
			//数据反馈
			boolean flag = uafs.userAdd(fau, userPwd2);
			
			
		}else if(op.equals("userUpdate")) {
			//获取员工Id
			String userId = request.getParameter("userId");
			//获取员工姓名
			String userName = request.getParameter("userName");
			//获取员工手机
			String userPhone = request.getParameter("phone");
			//获取员工邮箱 
			String userEmail = request.getParameter("userEmail");
			//获取员工地址
			String userAddress = request.getParameter("address");
			//获取员工身份证
			String userCardId = request.getParameter("cardId");
			//获取员工所属部门
			String userPost = request.getParameter("userPost");
			//对数据进行封装
			FilesAndUsers fau = new FilesAndUsers(Integer.parseInt(userId), userName, "", userPhone, userEmail, userAddress, userCardId, userPost);
			//数据反馈
			boolean flag = uafs.userUpdate(fau);

		}else if(op.equals("userDel")) {//员工管理的删除
			int CardId = 0;
			if(request.getParameter("CardId")!=null) {
				CardId = Integer.parseInt(request.getParameter("CardId"));
			}
			boolean flag = uafs.userDel(CardId);
			
		}else if(op.equals("postSel")) {//岗位管理的查询功能
			String posName = "";
			//判断传递的岗位名字是否为空
			if(request.getParameter("deptName")!=null) {
				//设置岗位管理的名字
				posName = request.getParameter("deptName");
			}
			//岗位管理的访问数据库
			List<Posts> list = psd.posSel(posName);
			
			//存储数据
			request.setAttribute("postList", list);
			//跳转页面
			request.getRequestDispatcher("dept_1.jsp").forward(request, response);
		}else if(op.equals("deptAdd")) {//岗位管理的添加管理
			//获取岗位名称
			String postname = request.getParameter("postname");
			//获取岗位职责
			String postWork = request.getParameter("postWork");
			Posts post = new Posts(0, postname, postWork, 0);
			boolean flag = psd.posAdd(post);
			
		}else if(op.equals("deptUpdate")) {
			//获取岗位编号
			String posId = request.getParameter("posId");
			//获取岗位名称
			String posName = request.getParameter("posName");
			//获取岗位职责
			String posWork = request.getParameter("posWork");
			Posts post = new Posts(Integer.parseInt(posId), posName, posWork,0);
			boolean flag = psd.posUpdate(post);
			System.out.println(flag);
		}else if(op.equals("deptDel")) {//岗位管理的删除功能
			//获取岗位编号
			String posId = request.getParameter("deptId");
			System.out.println("deptId : "+posId);
			//返回数据的岗位管理
			boolean flag = psd.posDel(Integer.parseInt(posId));
			System.out.println(flag);
			
		}else if(op.equals("zhousel")) {
			//上周的公司考勤统计功能
			int chidao = as.attencessel1().get(0).getAttId();
			int zaotui = as.attencessel2().get(0).getAttId();
			int jiaban = as.attencessel3().get(0).getAttId();
			int zhengchang = as.attencessel4().get(0).getAttId();
			List<Integer> list = new ArrayList();
			list.add(chidao);
			list.add(zaotui);
			list.add(jiaban);
			list.add(zhengchang);
//			System.out.println(list);
			request.setAttribute("list",list);
			request.getRequestDispatcher("attences_1.jsp").forward(request, response);
		}else if(op.equals("zhexiandenglu")) {
			//迟到人数
			List<Attences> list1 = as.Weekchidao1();
			//早退人数
			List<Attences> list2 = as.Weekzaotui1();
			//加班人数
			List<Attences> list3 = as.Weekjiaban1();
			//加班时长
			List<Attences> list4 = as.Weekjiatime1();
			//正常上班人数
			List<Attences> list5 = as.Weekzhengchang();
			
			request.setAttribute("list1",list1);
			request.setAttribute("list2",list2);
			request.setAttribute("list3",list3);
			request.setAttribute("list4",list4);
			request.setAttribute("list5",list5);
			
			request.getRequestDispatcher("personnelIndex.jsp").forward(request, response);
		}else if(op.equals("filesminsel")) {
			int curr = 1;
			int limit = 5;
			String strLike = "";
			if(request.getParameter("curr")!=null) {
				curr = Integer.parseInt(request.getParameter("curr"));
			}
			if(request.getParameter("limit")!=null) {
				limit = Integer.parseInt(request.getParameter("limit"));
			}
			if(request.getParameter("username2")!=null) {
				strLike = request.getParameter("username2");
			}
			NoticesDao nd = new NoticesDaoImp();
			PageData<FilesMin> pd  = fs.getselFilesminMinPage1(curr, limit, strLike);
			System.out.println("List : "+pd.getData());
			request.setAttribute("notices", pd);
			request.getSession().setAttribute("strLike", strLike);
			request.getRequestDispatcher("files_2.jsp").forward(request, response);
		}else if(op.equals("fileminpagesel1")) {
			int curr = 1;//页数
			int limit = 5;//每页的记录数
			String strLike = "";//模糊查询的字段
			if(request.getParameter("curr")!=null) {
				//获取jsp传递过来的页数参数
				curr = Integer.parseInt(request.getParameter("curr"));
			}
			if(request.getParameter("limit")!=null) {
				//获取jsp传递过来的每页记录数的参数
				limit = Integer.parseInt(request.getParameter("limit"));
			}
			if(request.getParameter("username2")!=null) {
				//获取jsp的模糊查询的参数的字段
				strLike = request.getParameter("username2");
				
			}
			//创建公告的实体类对象
			NoticesDao nd = new NoticesDaoImp();
			
			PageData<FilesMin> pd  = fs.getselFilesminMinPage1(curr, limit, strLike);
			request.setAttribute("notices", pd);
			request.getSession().setAttribute("strLike", strLike);
			request.getRequestDispatcher("files_2.jsp").forward(request, response);
		}else if(op.equals("departaddadd")) {
			String deptName = request.getParameter("deptName");
			int deptManId = Integer.parseInt(request.getParameter("deptManId"));
			String deptInfo = request.getParameter("deptInfo");
			Departments dept =new Departments(deptName,0,deptManId,deptInfo);
			boolean f = ds.Departmentsadd(dept);
		}
	}
	/**
	 * 公告管理系统通用查询
	 * @param request：请求编码
	 * @param response：响应编码
	 * @throws UnsupportedEncodingException
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selQuery(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, ServletException, IOException {
		/*//模糊查询的关键字段
		String notContent = "";
		
		//对公告表进行全查询
		List<Notices> list = ns.selNotices(notContent,1);
		//存储list<Notices>的属性
		request.setAttribute("notices", list);
		
		//Notices_1.js的跳转
		request.getRequestDispatcher("Noticespage_1.jsp").forward(request, response);*/
		int curr = 1;//页数
		int limit = 7;//每页的记录数
		String strLike = "";//模糊查询的字段
		if(request.getParameter("curr")!=null) {
			//获取jsp传递过来的页数参数
			curr = Integer.parseInt(request.getParameter("curr"));
		}
		if(request.getParameter("limit")!=null) {
			//获取jsp传递过来的每页记录数的参数
			limit = Integer.parseInt(request.getParameter("limit"));
		}
		if(request.getParameter("username2")!=null) {
			//获取jsp的模糊查询的参数的字段
			strLike = request.getParameter("username2");
			
		}
		//创建公告的实体类对象
		NoticesDao nd = new NoticesDaoImp();
		
		PageData<Notices> pd  = nd.selNoticesPage(curr,limit,strLike);
		request.setAttribute("notices", pd);
		request.getSession().setAttribute("strLike", strLike);
		request.getRequestDispatcher("Notices_1.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
